<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/8/24
 * Time: 13:45
 */

namespace ticket\modules\backend\controllers;

use ticket\libs\Tools;
use ticket\modules\backend\models\Application;
use Yii;


/*** 应用控制器
 * Class ApplicationController
 * @package ticket\modules\backend\controllers
 */
class ApplicationController extends BasicController
{


    /*** 应用列表
     * @return false|string
     * @throws \yii\db\Exception
     */
    public function actionApplicationList()
    {
        $get = Yii::$app->request->get();

        $page = isset($get['page'])?$get['page']:1;
        $pageSize = isset($get['size'])?$get['size']:10;
        $name = $get['name'];
        $app_id = $this->appId;
        if  (isset($name) and !empty($name)){
            $where = ['like','name','%'.$name.'%'];
        }else{
            $where = [];
        }
        $offset = $pageSize * ($page - 1);
        $searchModel = Application::find()->where(['app_id' => $app_id])->andWhere($where);
        $applicationList = $searchModel->offset($offset)->orderBy(['aid' => SORT_DESC])->asArray()->all();
        return Tools::showRes(0,'success',[
            'attrList' => $applicationList,
        ]);
    }


    /*** 添加应用
     * @return false|string
     */
    public function actionAddApplication()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $app_id = $this->appId;
            $appcationModel = new Application();
            if (!$appcationModel->addApplication($app_id,$post)){
                if ($appcationModel->hasErrors()){
                    return Tools::showRes(99,$appcationModel->getErrors());
                }else{
                    return Tools::showRes(99,'添加失败');
                }
            }

            return Tools::showRes(0,'success');
        }
    }


    /*** 修改应用
     * @return false|string
     */
    public function actionModApplication()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $appcationModel = new Application();
            $aid = isset($post['aid'])?$post['aid']:'';
            $app_id = $this->appId;
            if (!$appcationModel->modApplication($aid,$app_id,$post)){
                if ($appcationModel->hasErrors()){
                    return Tools::showRes(99,$appcationModel->getErrors());
                }else{
                    return Tools::showRes(99,'修改失败');
                }
            }

            return Tools::showRes(0,'success');

        }
    }

    /*** 获取应用
     * @return false|string
     */
    public function actionGetApplicationInfo()
    {
        $get = Yii::$app->request->get();
        $aid = isset($get['aid'])?$get['aid']:'';
        if  (empty($aid)){
            return Tools::showRes(99,'参数有误');
        }
        $appcationModel = new Application();
        $applicationInfo = $appcationModel->getApplicationInfo($aid);
        return Tools::showRes(0,'success',[
            'applicationInfo' => $applicationInfo,
        ]);
    }



    /*** 删除信息
     * @return false|string
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionDelApplication()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $aid = isset($post['aid'])?$post['aid']:'';
            if  (empty($aid)){
                return Tools::showRes(99,'参数有误');
            }
            $appcationModel = new Application();
            if ($appcationModel->delApplication($aid)){
                return Tools::showRes(0,'success');
            }
            return Tools::showRes(99,'删除失败');
        }
    }



}